# This code is hosted on http://code.google.com/p/lenthorp/
# Freely available for use in applications, but should NOT be modified
# Email all comments to lenthorpresearch@gmail.com

import PricingEngineMod
from PricingEngineMod import *
reload(PricingEngineMod)

from math import exp, log, sqrt, pow
from scipy.stats import norm
from numpy import interp

# modelParams: zeroRates
# otherParams: swapTenor, swaptionMaturity, nu, lambda
class HullWhite(PricingEngine):

    def getPrice(self):
        if (self.currentStatus > 0):
            op = self.otherParams.p
            mp = self.modelParams.p

            coterminal = op['swapTenor'] + op['swaptionMaturity']
            zc = interp([coterminal],mp['zeroRates']['tenors'],mp['zeroRates']['rates'])[0]
            zc1 = interp([op['swaptionMaturity']],mp['zeroRates']['tenors'],mp['zeroRates']['rates'])[0]
            
            val = sqrt(((op['nu']**2) * (1-exp(-2*op['lambda'] * op['swaptionMaturity'])) / (2*op['lambda']) * (((1-exp(-op['lambda'] * op['swapTenor'])) / op['lambda']) ** 2) * (zc1/(zc1-zc)) ** 2) / op['swaptionMaturity'])

            return val